package pierwszy;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import static java.lang.Math.abs;

public class KsPortalPage {
	private WebDriver driver;
	private WebDriverWait wait;
	private List<By> usersLocatorsList= new ArrayList<By>();
	private List<By> clientsLocatorsList= new ArrayList<By>();
	private int TranslationId;
	private Integer ostatnieIDZleceniaUS = 0;
//	private Integer ostatnieIDNowegoPodatku = 0;
	
	KsPortalPage(WebDriver driver, WebDriverWait wait){
		this.driver = driver;
		this.wait = wait;
		
		if(!driver.getTitle().equals("Centrum Ksiegowości i Edukacji Finansowej - panel administratora")){
			throw new NoSuchWindowException("This is not the KsPortal page");
		}	
	}
	
	public LogInPage LogOutKsPortalPage(){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.LogOutButtonKsPortalPage));
		WebElement lgo = driver.findElement(UiMapping.LogOutButtonKsPortalPage);
		lgo.click();
		
		if (!driver.getTitle().equals("mAdmin- LOGOWANIE DO SYSTEMU")){
			throw new NoSuchWindowException("This is not the Login page");
		}
		
		return new LogInPage(driver, wait);
	}
	
	public void getOptionFromIconPlus(String optionIconPlus){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.IconPlus));
		WebElement ikonaPlus = driver.findElement(UiMapping.IconPlus);
		ikonaPlus.click();
		
		WebElement note = driver.findElement(UiMapping.passOptionOfIconPlus(optionIconPlus));
		wait.until(ExpectedConditions.elementToBeClickable(note));
		note.click();
	}
	
	//Note
	public void addNote(String noteCategory, String noteContent){
		System.out.println("addNote");
		
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passNoteCategory(noteCategory)));
		WebElement category = driver.findElement(UiMapping.passNoteCategory(noteCategory));
		category.click();

		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.NoteTextArea));
		WebElement textArea = driver.findElement(UiMapping.NoteTextArea);
		textArea.sendKeys(noteContent);
		
		WebElement zapisBtn = driver.findElement(UiMapping.NoteSaveButton);
		zapisBtn.click();
	}
	
	public void chooseNoteCategory(String noteCategory){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.NoteCategoryList));
		WebElement noteCatList = driver.findElement(UiMapping.NoteCategoryList);
		noteCatList.click();
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passNoteFilterCategory(noteCategory)));
		WebElement noteCat = driver.findElement(UiMapping.passNoteFilterCategory(noteCategory));
		noteCat.click();
	}
	
	public void removeNote(String noteContent) throws InterruptedException{
		System.out.println("removeNote");
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passNoteContentButton(noteContent)));
		WebElement usunBtn = driver.findElement(UiMapping.passNoteContentButton(noteContent));
		usunBtn.click();
		
		Thread.sleep(2000);
		driver.switchTo().alert().accept();	
	}
	
	//News
	public List<WebElement> fillInNews(String title, String shortage, String text) throws InterruptedException{		
		System.out.println("addNews");
		
		//setTitle
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.NewsTitle));
		WebElement titleTextArea = driver.findElement(UiMapping.NewsTitle);
		titleTextArea.sendKeys(title);
		
		//setShortage
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.NewsShortage));
		WebElement shortageTextArea = driver.findElement(UiMapping.NewsShortage);
		shortageTextArea.sendKeys(shortage);
		
		//setText		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.NewsTextArea));
		driver.switchTo().frame(driver.findElement(UiMapping.NewsTextArea));
		WebElement textTextAreaIFrame = driver.findElement(By.xpath("//html/body"));
		textTextAreaIFrame.sendKeys(text); 
		driver.switchTo().defaultContent();
		
		return driver.findElements(UiMapping.NewsDateTime);
	}
	
	
	public void submitNews(){
		//submit
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.NewsSubmitButton));
		WebElement submitBtn = driver.findElement(UiMapping.NewsSubmitButton);
		submitBtn.click();
	}
	
	
	public List<String> getDateTimeOfNews(List<WebElement> DateTimeWebElementsList){
		List <String> DateTimeList = new ArrayList<String>();
		System.out.println("Długość listy DateTimeList: " + DateTimeWebElementsList.size());
		
		for (WebElement dt : DateTimeWebElementsList){
			System.out.println("Dupa:" + dt.getAttribute("value"));
			DateTimeList.add(dt.getAttribute("value"));
		}
		return DateTimeList;
	}
	
	public long getDatesDifference (String dateAddition, String dateCheck) throws ParseException{
		SimpleDateFormat sDF = new SimpleDateFormat("yyyy-MM-dd, kk:mm:ss");
		Date date1 = sDF.parse(dateAddition);
		Date date2 = sDF.parse(dateCheck);
		System.out.println("Data  add: " + dateAddition);
		System.out.println("Data  check: " + dateCheck);
		
		System.out.println("Różnica dat w s: " + ((date1.getTime() - date2.getTime())/1000));
		return abs((date1.getTime() - date2.getTime())/1000);
	}
	
	//find news using title, date and time of news insertion
	public WebElement findNewsByTitleAndDate(String Title, List<String> DateTimeList){
		final Long timeDifferece = 2L;
		
		//Get list of all notes with specified title date and time.
		List <WebElement> FoundNewsElementsList = driver.findElements(UiMapping.passNewsTitle(Title, DateTimeList.get(0)));
		System.out.println("Znalezione elementy o podanej dacie i tytule: " + FoundNewsElementsList.size());
		
		for (int i = (FoundNewsElementsList.size() -1) ; i >= 0 ; i--){
			//Choose note number i
			FoundNewsElementsList.get(i).click();
			
			//Wait for and find return button
			wait.until(ExpectedConditions.elementToBeClickable(UiMapping.NewsReturnBtn));
			WebElement newsReturn = driver.findElement(UiMapping.NewsReturnBtn);
			
			//Check date and time of added news
			wait.until(ExpectedConditions.elementToBeClickable(UiMapping.NewsDateTimeCheck));
			WebElement newsDateTimeCheck = driver.findElement(UiMapping.NewsDateTimeCheck);
		
			String dateOfAddition = DateTimeList.get(0).concat(", ").concat(DateTimeList.get(1));
			String dateOfCheck = newsDateTimeCheck.getText().substring(newsDateTimeCheck.getText().indexOf(":") + 1);
			System.out.println("Data i czas checked: " + dateOfCheck);
			System.out.println("Data i czas pobrane z przy dodawaniu: " + dateOfAddition);
			
			try {
				if(getDatesDifference(dateOfAddition, dateOfCheck) <= timeDifferece){
					System.out.println("Odnaleziono wiadomość" + Title + DateTimeList.get(0) + DateTimeList.get(1));
					return FoundNewsElementsList.get(i);
				}
				else{
					newsReturn.click();
					System.out.println("Wiadomość"  + i + "of" + FoundNewsElementsList.size() + "nie zgodna");
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
						
			//Get list of all notes with specified title date and time.
			FoundNewsElementsList = driver.findElements(UiMapping.passNewsTitle(Title, DateTimeList.get(0)));
		}
		return null;
	}
	
	public void removeNews(){
		//removeNews
		System.out.println("removeNews");
		wait.until(ExpectedConditions.elementToBeClickable(UiMapping.NewsRemoveButton));
		WebElement newsRemoveBtn = driver.findElement(UiMapping.NewsRemoveButton);
		
		newsRemoveBtn.click();
		driver.switchTo().alert().accept();
	}
	
	
	public int getNewsCount(){
		wait.until(ExpectedConditions.elementToBeClickable(UiMapping.NewsCount));
		return Integer.parseInt(driver.findElement(UiMapping.NewsCount).getText());		
	}
	
	
	//User
	public void setNewUser(String... data){
        WebElement userElement;
		usersLocatorsList = UiMapping.listOfLocatorsForUser(data[6]);
		
		for (int i = 0 ; i < data.length ; i++){
			wait.until(ExpectedConditions.elementToBeClickable(usersLocatorsList.get(i)));
			userElement = driver.findElement(usersLocatorsList.get(i));
			if(i != 6){
				userElement.sendKeys(data[i]);
			}
			else{
				userElement.click();
			}	
		}
	}
	
	public void cleanUserInfo(){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.cleanEmplyeeBtn));
		WebElement cleanBtn = driver.findElement(UiMapping.cleanEmplyeeBtn);
		cleanBtn.click();
	}
	
	
	public void addNewUser(){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.addEmployeeBtn));
		WebElement cleanBtn = driver.findElement(UiMapping.addEmployeeBtn);
		cleanBtn.click();
	}
	
	public Boolean checkIfCleaned(String... data){
        WebElement userElement;
		usersLocatorsList = UiMapping.listOfLocatorsForUser(data[6]);
		
		
		for (int i = 0 ; i < data.length ; i++){
			wait.until(ExpectedConditions.elementToBeClickable(usersLocatorsList.get(i)));
			userElement = driver.findElement(usersLocatorsList.get(i));
			if(i != 6){
				if(!userElement.getText().isEmpty()){
					return false;
				}
			}
			else if(userElement.isSelected()){
				return false;
			}	
		}
		return true;
	}
	
	
	public boolean setNewEmployeeLocation(Map<String,String> locationFiledNamesValuesMap){
		WebElement reservation;
		List<WebElement> locationFieldNames= new LinkedList<WebElement>();
		List<WebElement> locationFieldValues= new LinkedList<WebElement>();
//		Map<String,String> locaionFiledNamesValuesMap = new TreeMap<String,String>();
//		locaionFiledNamesValuesMap.put("Nazwa:", "A");
//		locaionFiledNamesValuesMap.put("Ulica:", "B");
//		locaionFiledNamesValuesMap.put("Numer lokalu:", "C");
//		locaionFiledNamesValuesMap.put("Numer budynku:", "4d");
//		locaionFiledNamesValuesMap.put("Miasto:", "D D");
//		locaionFiledNamesValuesMap.put("Kod pocztowy:", "90-510");
//		locaionFiledNamesValuesMap.put("Poczta:", "D D");
//		locaionFiledNamesValuesMap.put("Ilość miejsc:", "12");
//		locaionFiledNamesValuesMap.put("Opis:", "opis lokalizaji");
//		locaionFiledNamesValuesMap.put("Wyposażenie:", "komputer, internet");
//		locaionFiledNamesValuesMap.put("Podlega rezerwacji:", "tak");
		
		//click on Location button
		wait.until(ExpectedConditions.elementToBeClickable(UiMapping.employeeLocation));
		WebElement employeeLocationButton = driver.findElement(UiMapping.employeeLocation);
		employeeLocationButton.click();
		
		//choose "[+] Lokalizacje" from location menu list
		wait.until(ExpectedConditions.elementToBeClickable(UiMapping.locationMenuList));
		WebElement locationMenuListBtn = driver.findElement(UiMapping.locationMenuList);
		locationMenuListBtn.click();
		
		wait.until(ExpectedConditions.elementToBeClickable(UiMapping.addLocationListOption));
		WebElement addLocationOptionBtn = driver.findElement(UiMapping.addLocationListOption);
		addLocationOptionBtn.click();
		
		locationFieldNames = driver.findElements(UiMapping.employeeLocationFieldNames);
		locationFieldValues = driver.findElements(UiMapping.employeeLocationFieldValues);
		
		
		if(locationFieldNames.size() == locationFieldValues.size()){
			for(int i=0; i<locationFieldNames.size();i++){
				if(locationFiledNamesValuesMap.containsKey(locationFieldNames.get(i).getText())){
					locationFieldValues.get(i).sendKeys(locationFiledNamesValuesMap.get(locationFieldNames.get(i).getText()));
				}
				else{
					return false;
				}
			}
		}
		else{
			return false;
		}
		
		reservation = locationFieldValues.get(locationFieldValues.size()-1);
		
		
		if(locationFiledNamesValuesMap.get("Podlega rezerwacji:").toLowerCase().equals("tak") && !locationFieldValues.get(locationFieldValues.size()-1).isSelected()){
			reservation.click();
		}
		else if(locationFiledNamesValuesMap.get("Podlega rezerwacji:").toLowerCase().equals("nie") && locationFieldValues.get(locationFieldValues.size()-1).isSelected()){
			reservation.click();
		}
		
		WebElement addLocationBtn = driver.findElement(UiMapping.addLocationBtn);
		addLocationBtn.click();
		
		
		return true;
	}
	
	public WebElement findLastLocation(){
		int lastId = 0;
		wait.until(ExpectedConditions.elementToBeClickable(UiMapping.locationIds));
		List<WebElement> locationIds = driver.findElements(UiMapping.locationIds);
		
		for (WebElement locationId : locationIds){
			lastId = Math.max(lastId, Integer.parseInt(locationId.getAttribute("href").substring(locationId.getAttribute("href").lastIndexOf("id=") + 3)));
		}
		System.out.println("max:" + lastId);
		
		WebElement lastLocation = driver.findElement(UiMapping.passLastLocationId(lastId));
		
		//lastLocation.click();
		
		return lastLocation;
	}
	
	public void openLocation(WebElement locationWebElement){
		locationWebElement.click();
	}
	
	public String getLocationContent(){
		wait.until(ExpectedConditions.elementToBeClickable(UiMapping.locationContent));
		String locationTextContent = driver.findElement(UiMapping.locationContent).getText();
		
		System.out.println("Location text: " + locationTextContent);
		
		return locationTextContent;
	}
	
	
	public String getLocationDescription(){
		wait.until(ExpectedConditions.elementToBeClickable(UiMapping.locationOpisBtn));
		WebElement locationOpisBtn = driver.findElement(UiMapping.locationOpisBtn);
		locationOpisBtn.click();
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.locationOpis));
		String locationTextOpis = driver.findElement(UiMapping.locationOpis).getText();
		
		System.out.println("Location opis: " + locationTextOpis);
		
		return locationTextOpis;
	}
	
	
	public WebElement findEmployeeByLoginAndName(String login, String firstName, String lastName){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passUserLoginAndName(login, firstName, lastName)));
		WebElement foundEmployee = driver.findElement(UiMapping.passUserLoginAndName(login, firstName, lastName));
		
		return foundEmployee;
	}
	
	public void employeeChooseOption(String login, String firstName, String lastName, String option){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passOptionsOfEmployeeBtn(login, firstName, lastName)));
		WebElement employeeOptionsBtn = driver.findElement(UiMapping.passOptionsOfEmployeeBtn(login, firstName, lastName));
		
		employeeOptionsBtn.click();
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passUserLoginNameAndOption(login, firstName, lastName, option)));
		WebElement foundEmployeeOption = driver.findElement(UiMapping.passUserLoginNameAndOption(login, firstName, lastName, option));
	
		foundEmployeeOption.click();
		
		driver.switchTo().alert().accept();	
	}

	
	//Client
	
	//choose client type (available types: "firma", "osoba fizyczna")
	public boolean chooseClientType(String clientType){
		if(clientType=="osoba fizyczna" || clientType=="firma"){
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passClientType(clientType)));
			WebElement chosenClientType = driver.findElement(UiMapping.passClientType(clientType));
			chosenClientType.click();
		}
		else {
			return false;
		}
		return true;
	}
	
	public boolean checkClienEmailLogin(String clientType, String clientEmailLogin){
		if(clientType == "osoba fizyczna"){
			UiMapping.passClientEmailLogin("Login");
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passClientEmailLogin("Login")));
			WebElement clientLogin = driver.findElement(UiMapping.passClientEmailLogin("Login"));
			clientLogin.sendKeys(clientEmailLogin);
			
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.loginAvailibilityBtn));
			WebElement checkLoginAvailibilityBtn = driver.findElement(UiMapping.loginAvailibilityBtn);
			checkLoginAvailibilityBtn.click();
			
			try{
				wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passClientEmailLoginAvailibility(clientEmailLogin)));
				System.out.println("This login is available.");
			}
			catch(TimeoutException a){
				wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.loginAvailibility));
				System.out.println("This login is NOT available!");
				return false;
			}
		}
		else if(clientType == "firma") {
			UiMapping.passClientEmailLogin("EMAIL");
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passClientEmailLogin("EMAIL")));
			WebElement clientEmail = driver.findElement(UiMapping.passClientEmailLogin("EMAIL"));
			clientEmail.sendKeys(clientEmailLogin);
			
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.emailAvailibilityBtn));
			WebElement checkEmailAvailibilityBtn = driver.findElement(UiMapping.emailAvailibilityBtn);
			checkEmailAvailibilityBtn.click();
			
			try{
				wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passClientEmailLoginAvailibility(clientEmailLogin)));
				System.out.println("This email address is available.");
			}
			catch(TimeoutException a){
				wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.emailAvailibility));
				if(driver.findElement(UiMapping.emailAvailibility).getText().trim().contains("Firma o podanym adresie email już istnieje")){
					System.out.println("This email address is NOT available.");
				}
				else if (driver.findElement(UiMapping.emailAvailibility).getText().trim().contains("Niepoprawny adres email")){
					System.out.println("Wrong email address format.");
				}
				return false;
			}
		}
		return true;
	}
	
	public void setCompanyClientData(String[][] companyClientData){
    	clientsLocatorsList = UiMapping.listOfLocatorsForClientCompany();
    	WebElement userElement;
    	//WebElement nextBtn;
    	
    	wait.until(ExpectedConditions.elementToBeClickable(UiMapping.nextCompanyClientBtn));
		List <WebElement> nextBtns = driver.findElements(UiMapping.nextCompanyClientBtn);
		System.out.println("rozmiar" + nextBtns.size());
    	
    	for(int siteNumber = 0; siteNumber < companyClientData.length; siteNumber++){
    		for(int fieldNumber = 0 ; fieldNumber < companyClientData[siteNumber].length; fieldNumber++){
    			wait.until(ExpectedConditions.elementToBeClickable(clientsLocatorsList.get(fieldNumber)));
    			userElement = driver.findElement(clientsLocatorsList.get(fieldNumber));
    			userElement.sendKeys(companyClientData[siteNumber][fieldNumber]);
    	}
		
    		if(siteNumber <5){
    			nextBtns.get(siteNumber).click();;
    		}
    	}
	}
	
	public void setClientData(String clientType, String... clientData){
        WebElement userElement;
        int nextPageMemory = 0;
        int fieldNumber = 0;
        
        if(clientType.equals("osoba fizyczna")){
        	clientsLocatorsList = UiMapping.listOfLocatorsForClientNaturalPerson();
        
        	for (int i = 1 ; i < clientData.length ; i++){
        		wait.until(ExpectedConditions.elementToBeClickable(clientsLocatorsList.get(i)));
        		userElement = driver.findElement(clientsLocatorsList.get(i));
        		userElement.sendKeys(clientData[i]);
        	}
        }
        else if(clientType.equals("firma")){
        	clientsLocatorsList = UiMapping.listOfLocatorsForClientCompany();
        	for (int siteNumber = 2; siteNumber < 6; siteNumber ++){
				for (fieldNumber = nextPageMemory ; fieldNumber < clientData.length ; fieldNumber++){
        			wait.until(ExpectedConditions.elementToBeClickable(clientsLocatorsList.get(fieldNumber)));
        			userElement = driver.findElement(clientsLocatorsList.get(fieldNumber));
        			userElement.sendKeys(clientData[fieldNumber]);
        		}
        		nextPageMemory = fieldNumber;
        		
        		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.nextCompanyClientBtn));
    			WebElement nextBtn = driver.findElement(UiMapping.nextCompanyClientBtn);
    			nextBtn.click();
    			
    			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.addClientCheckNextPage(Integer.toString(siteNumber))));	
        	}
        }
	}
	
	
	public void addNewNaturalPersonClient(){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.addNaturalPersonBtn));
		WebElement addBtn = driver.findElement(UiMapping.addNaturalPersonBtn);
		addBtn.click();
	}
	
	public void addNewCompanyClient(){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.addCompanyBtn));
		WebElement addBtn = driver.findElement(UiMapping.addCompanyBtn);
		addBtn.click();
	}
	
	public WebElement findNaturalPersonByNameAndLogin (String[] NaturalPersonData){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passNaturalPersonNameAndLogin(NaturalPersonData)));
		WebElement FoundedNaturalPerson = driver.findElement(UiMapping.passNaturalPersonNameAndLogin(NaturalPersonData));
		return FoundedNaturalPerson;	
	}
	
	public void removeNaturalPersonByNameAndLogin (String[] NaturalPersonData){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passNaturalPersonNameAndLogin(NaturalPersonData)));
		WebElement FoundedNaturalPerson = driver.findElement(UiMapping.passNaturalPersonNameAndLogin(NaturalPersonData));
		FoundedNaturalPerson.click();
		driver.switchTo().alert().accept();	
	}
	
	public WebElement findCompanyByName(String[][] CompanyName){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passCompanyName(CompanyName[0][2])));
		WebElement FoundedCompany = driver.findElement(UiMapping.passCompanyName(CompanyName[0][2]));
		return FoundedCompany;
	}
	
	public String getAmountOfElements(){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.AmountOfElements));
		WebElement AmountOfCompanies = driver.findElement(UiMapping.AmountOfElements);
		return AmountOfCompanies.getText();
	}
	
	public void removeFoundedComapny(){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.removeCompanyBtn));
		WebElement removeBtn = driver.findElement(UiMapping.removeCompanyBtn);
		removeBtn.click();
		driver.switchTo().alert().accept();	
	}
	
	public void fillCommissionTranslationForm(String category, Map<String,String> commissionTranslationData){	
		Select select = new Select(driver.findElement(By.id("id_zlecenia_cat")));
		//select.deselectAll();
		select.selectByVisibleText(category);
		
		//Fill in title
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.commissionTranlationTitle));
		WebElement translationTitle = driver.findElement(UiMapping.commissionTranlationTitle);
		translationTitle.sendKeys(commissionTranslationData.get("Title"));
		
		//Fill in description
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.commissionTranlationDescription));
		driver.switchTo().frame("idContentofcontentpl");
		WebElement translationDescription = driver.findElement(UiMapping.commissionTranlationDescriptionIFrame);
		translationDescription.sendKeys(commissionTranslationData.get("Description"));
		driver.switchTo().defaultContent();	
		
		//Fill in date
//		String portalWindowHandle = driver.getWindowHandle();
//		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.commissionTranlationCalendar));
//		WebElement commissionCalendar = driver.findElement(UiMapping.commissionTranlationCalendar);
//		commissionCalendar.click();
//		
//		driver.switchTo().window("Calendar");
//		System.out.println("window handle calendar: "+driver.getWindowHandle());
//		WebElement commissionDate = driver.findElement(UiMapping.passCommissionDate("wstaw date"));
//		commissionDate.click();
//		
//		driver.switchTo().window(portalWindowHandle);
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.comissionTranslationSetDate));
		WebElement comissionSetDate = driver.findElement(UiMapping.comissionTranslationSetDate);
		comissionSetDate.clear();
		comissionSetDate.sendKeys(commissionTranslationData.get("Date"));
		
		//Fill in time
		Select selectHour = new Select(driver.findElement(By.id("godzina")));
		//select.deselectAll();
		selectHour.selectByVisibleText(commissionTranslationData.get("Hour"));
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.commissionTranlationMinutes));
		WebElement commissionMinutes = driver.findElement(UiMapping.commissionTranlationMinutes);
		commissionMinutes.clear();
		commissionMinutes.sendKeys(commissionTranslationData.get("Minute"));
		
		//Fill in customer
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.comissionTranslationDefineCustomer));
		WebElement commissionCustomerBtn = driver.findElement(UiMapping.comissionTranslationDefineCustomer);
		commissionCustomerBtn.click();
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passComissionTranslationCustomerName(commissionTranslationData.get("Customer"))));
		WebElement commissionSelectCustomer = driver.findElement(UiMapping.passComissionTranslationCustomerName(commissionTranslationData.get("Customer")));
		commissionSelectCustomer.click();

		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.comissionTranslationSetCustomer));
		WebElement comissionSetCustomer = driver.findElement(UiMapping.comissionTranslationSetCustomer);
		comissionSetCustomer.click();
		
		//Fill in accountant
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.comissionTranslationDefineAccountant));
		WebElement commissionAccountantBtn = driver.findElement(UiMapping.comissionTranslationDefineAccountant);
		commissionAccountantBtn.click();
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passComissionTranslationAccountantName(commissionTranslationData.get("Accountant"))));
		WebElement commissionSelectAccountant = driver.findElement(UiMapping.passComissionTranslationAccountantName(commissionTranslationData.get("Accountant")));
		commissionSelectAccountant.click();

		//commissionSelectAccountant.submit();
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.comissionTranslationSetAccountant));
		WebElement comissionSetAccountant = driver.findElement(UiMapping.comissionTranslationSetAccountant);
		comissionSetAccountant.click();
	}
	
	public void submitCommissionTranslationForm(){	
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.comissionTranslationSubmit));
		WebElement comissionSetAccountant = driver.findElement(UiMapping.comissionTranslationSubmit);
		comissionSetAccountant.click();
	}
	
	public Boolean findCommissionTranslationLastId(Map<String,String> commissionTranslationData){
		List<WebElement> commissionSites = driver.findElements(UiMapping.comissionTranslationFindSites);
		List<WebElement> commissions = driver.findElements(UiMapping.comissionTranslationFind);
		System.out.println("ilosc stron" + commissionSites.size());

		int max = 0;
		int siteWithMax = 1;
		int maxSiteMem = 0;
		for (int i = 0; i< commissionSites.size(); i++) {
			commissionSites.get(i).click();
			
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.comissionTranslationFindSites));
			commissionSites = driver.findElements(UiMapping.comissionTranslationFindSites);
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.comissionTranslationFind));
			commissions = driver.findElements(UiMapping.comissionTranslationFind);
			
			for (WebElement commission : commissions) {
//				System.out.println("ID:" + commission.getText().trim());
				max = Math.max(Integer.parseInt(commission.getText().trim()),max);
			} 
			if(maxSiteMem<max){
				maxSiteMem=max;
				siteWithMax = i;
			}
//			System.out.println("SWM: " + siteWithMax);
//			System.out.println("max:" + max + "site " + i);
		}
		commissionSites.get(siteWithMax).click();
		System.out.println("max:" + max);
		TranslationId = max;
		try{
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passComissionTranslationAccountantName(max, commissionTranslationData.get("Date"), commissionTranslationData.get("Title"))));
			WebElement foundCommission = driver.findElement(UiMapping.passComissionTranslationAccountantName(max, commissionTranslationData.get("Date"), commissionTranslationData.get("Title")));				
			foundCommission.click();
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public Boolean findCommissionTranslationById(int ID){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.comissionTranslationFindSites));
		List<WebElement> commissionSites = driver.findElements(UiMapping.comissionTranslationFindSites);
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.comissionTranslationFind));
		List<WebElement> commissions = driver.findElements(UiMapping.comissionTranslationFind);
		System.out.println("ilosc stron" + commissionSites.size());

		for (int i = 0; i< commissionSites.size(); i++) {
			commissionSites.get(i).click();
			
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.comissionTranslationFindSites));
			commissionSites = driver.findElements(UiMapping.comissionTranslationFindSites);
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.comissionTranslationFind));
			commissions = driver.findElements(UiMapping.comissionTranslationFind);
			
			for (WebElement commission : commissions) {
				System.out.println("IDbyID:" + commission.getText().trim());
				if(commission.getText().trim().equals(Integer.toString(ID))){
					System.out.println("Dupa:" + commission.getText().trim());
					return false;
				}
			} 
		}	
		return true;
	}
	
	public Boolean checkCommisionTranslationData(String commissionCategory, Map<String,String> commissionTranslationData){
		Boolean commissionDataCheckFlag = true;
		List<WebElement> commisionDataToCheck = new ArrayList<WebElement>();
		commisionDataToCheck = driver.findElements(UiMapping.commissionDataCheck);
		
		for (WebElement commission : commisionDataToCheck) {
	    System.out.println(commission.getText().trim());
	}

		commissionDataCheckFlag &= commisionDataToCheck.get(0).getText().trim().equals(commissionTranslationData.get("Title"));
		if (!commissionDataCheckFlag){
			System.out.println("Titles are different!"); 
			return false;
		}
		
		commissionDataCheckFlag &= commisionDataToCheck.get(1).getText().trim().contains(commissionTranslationData.get("Date"));
		if (!commissionDataCheckFlag){
			System.out.println("Dates are different!"); 
			return false;
		}
		
		commissionDataCheckFlag &= commisionDataToCheck.get(1).getText().trim().contains(commissionTranslationData.get("Hour") + ":");
		if (!commissionDataCheckFlag){
			System.out.println("Hours are different!"); 
			return false;
		}
		
		commissionDataCheckFlag &= commisionDataToCheck.get(1).getText().trim().contains(":" + commissionTranslationData.get("Minute"));
		if (!commissionDataCheckFlag){
			System.out.println("Minutes are different!"); 
			return false;
		}
		
		commissionDataCheckFlag &= commisionDataToCheck.get(3).getText().trim().contains(commissionCategory);
		if (!commissionDataCheckFlag){
			System.out.println("Categories are different!"); 
			return false;
		}
		
		commissionDataCheckFlag &= commisionDataToCheck.get(4).getText().substring(commisionDataToCheck.get(4).getText().indexOf(":")).trim().contains(commissionTranslationData.get("Customer"));
		if (!commissionDataCheckFlag){
			System.out.println("Customers are different!"); 
			return false;
		}
		
		commissionDataCheckFlag &= commissionTranslationData.get("Accountant").contains(commisionDataToCheck.get(6).getText().substring(commisionDataToCheck.get(6).getText().indexOf(":")+1));
		if (!commissionDataCheckFlag){
			System.out.println("Accountants are different!"); 
			return false;
		}
		
		return commissionDataCheckFlag;
	}
	
	public void removeCommissionTranslation() {
		wait.until(ExpectedConditions.elementToBeClickable(UiMapping.removeTranslationBtn));
		driver.findElement(UiMapping.removeTranslationBtn).click();
		driver.switchTo().alert().accept();
	}
	
	public int getTranslationId() {
		return TranslationId;
	}
	
	
	public void wypelnijZlecenieUS(Map<String,String> daneZlecenieUS){	
		//Fill in customer
		Select select;
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.zlecenieUSWybierzKlienta));
		WebElement commissionCustomerBtn = driver.findElement(UiMapping.zlecenieUSWybierzKlienta);
		commissionCustomerBtn.click();
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.zlecenieUSAbonentPrzycisk));
		WebElement zlecenieUSAbonent = driver.findElement(UiMapping.zlecenieUSAbonentPrzycisk);
		zlecenieUSAbonent.click();
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passComissionTranslationCustomerName(daneZlecenieUS.get("Klient"))));
		WebElement commissionSelectCustomer = driver.findElement(UiMapping.passComissionTranslationCustomerName(daneZlecenieUS.get("Klient")));
		commissionSelectCustomer.click();
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.comissionTranslationSetCustomer));
		WebElement comissionSetCustomer = driver.findElement(UiMapping.comissionTranslationSetCustomer);
		comissionSetCustomer.click();
		
//		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.zlecenieUSZlecenia));
//		WebElement powiazanieZlecenia = driver.findElement(UiMapping.zlecenieUSZlecenia);
//		powiazanieZlecenia.click();
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passZlecenieUSZlecenia(daneZlecenieUS.get("Zlecenia"))));
		WebElement powiazanieZlecenia = driver.findElement(UiMapping.passZlecenieUSZlecenia(daneZlecenieUS.get("Zlecenia")));
		powiazanieZlecenia.click();
		
		select = new Select(driver.findElement(By.name("id_cat")));
		select.selectByVisibleText(daneZlecenieUS.get("Rodzaj podatku"));
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.zlecenieUSPolaInput));
		List<WebElement> polaTypuInput = driver.findElements(UiMapping.zlecenieUSPolaInput);

		for (WebElement aa: polaTypuInput){
			System.out.println(aa.getText()+", ");
		}
		
		polaTypuInput.get(0).sendKeys(daneZlecenieUS.get("Płatnik"));
		polaTypuInput.get(3).sendKeys(daneZlecenieUS.get("Rok"));
		polaTypuInput.get(4).sendKeys(daneZlecenieUS.get("Kwota"));
		polaTypuInput.get(5).sendKeys(daneZlecenieUS.get("Do kiedy zapłacić"));
		
		if(daneZlecenieUS.get("Typ")=="miesieczny"){
			polaTypuInput.get(1).click();
			select = new Select(driver.findElement(By.name("misiac")));
			select.selectByVisibleText(daneZlecenieUS.get("Miesiąc"));
		}
		else if (daneZlecenieUS.get("Typ")=="kwartalny"){
			polaTypuInput.get(2).click();
			select = new Select(driver.findElement(By.name("kwartal")));
			select.selectByVisibleText(daneZlecenieUS.get("Kwartał"));
		}
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.zleceniaUSUwagi));
		WebElement uwagiZlecenia = driver.findElement(UiMapping.zleceniaUSUwagi);
		uwagiZlecenia.sendKeys(daneZlecenieUS.get("Uwagi"));
	}
	
	public void dodajZlecenieUS(){	
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.zleceniaUSDodaj));
		WebElement zlecenieUSDodaj = driver.findElement(UiMapping.zleceniaUSDodaj);
		zlecenieUSDodaj.click();
	}
	
	public Boolean sprawdzPolaZleceniaUS(Map<String,String> daneZlecenieUS){
		Boolean flagaZgodnosciZleceniaUS = true;
		String opisPola = "";
		String wartoscPola = "";
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.zlecenieUSPolaDoSprawdzenia));
		List<WebElement> polaZleceniaUS = driver.findElements(UiMapping.zlecenieUSPolaDoSprawdzenia);
		 
		for(int i=0; i<(polaZleceniaUS.size() - 2); i++){
			opisPola = polaZleceniaUS.get(i).getText().substring(0,polaZleceniaUS.get(i).getText().indexOf(":")).trim();			
			wartoscPola = polaZleceniaUS.get(i).getText().substring(polaZleceniaUS.get(i).getText().indexOf(":") + 1, polaZleceniaUS.get(i).getText().length()).trim();
	
			System.out.println("Wartosc Pola: " + daneZlecenieUS.get(opisPola));
			System.out.println("Wartosc Pola: " + polaZleceniaUS.get(i).getText());
			
			if(i==1){
				System.out.println("True: " + wartoscPola.contains(daneZlecenieUS.get(opisPola)));
				flagaZgodnosciZleceniaUS &= wartoscPola.contains(daneZlecenieUS.get(opisPola));
			}
			else{
				System.out.println("True:" + daneZlecenieUS.get(opisPola).equals(wartoscPola));
				flagaZgodnosciZleceniaUS &= daneZlecenieUS.get(opisPola).equals(wartoscPola);
			}
		}
		return flagaZgodnosciZleceniaUS;
	}
	
	public void znajdzOstatnieZlecenieSkladke(String zlecenieSkladka){
		int siteWithMax = 1;
		int maxSiteMem = 0;
		if(zlecenieSkladka.toLowerCase().equals("zlecenieus")){
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.listaZlecenUS));
			WebElement listaZlecenPrzycisk = driver.findElement(UiMapping.listaZlecenUS);
			listaZlecenPrzycisk.click();
		}
		else if(zlecenieSkladka.toLowerCase().equals("skladkazus")){
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.listaSkladekZusBtn));
			WebElement listaSkladekPrzycisk = driver.findElement(UiMapping.listaSkladekZusBtn);	
			listaSkladekPrzycisk.click();
		}
			
		List<WebElement> stronyZlecenUS = driver.findElements(UiMapping.stronyZlecenieUS);
		List<WebElement> zleceniaUS = driver.findElements(UiMapping.zleceniaUSNaStronie);
		System.out.println("ilosc stron" + stronyZlecenUS.size());

		for (int i = 0; i< stronyZlecenUS.size(); i++) {
			stronyZlecenUS.get(i).click();
			
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.stronyZlecenieUS));
			stronyZlecenUS = driver.findElements(UiMapping.stronyZlecenieUS);
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.zleceniaUSNaStronie));
			zleceniaUS = driver.findElements(UiMapping.zleceniaUSNaStronie);
			
			for (WebElement zlecenieUS : zleceniaUS) {
			//System.out.println("ID:" + zlecenieUS.getText().trim());
				ostatnieIDZleceniaUS = Math.max(Integer.parseInt(zlecenieUS.getText().trim()),ostatnieIDZleceniaUS);
			} 
			if(maxSiteMem<ostatnieIDZleceniaUS){
				maxSiteMem=ostatnieIDZleceniaUS;
				siteWithMax = i;
			}
		}
		stronyZlecenUS.get(siteWithMax).click();
		System.out.println("max:" + ostatnieIDZleceniaUS);
		TranslationId = ostatnieIDZleceniaUS;
	}
	
	public boolean sprawdzOstatnieZlecenieUS(Map<String,String> daneZlecenieUS){
		String daneWpisane = "";
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passIdUSZlecenia(ostatnieIDZleceniaUS)));
		WebElement ostatnieZlecenieUS = driver.findElement(UiMapping.passIdUSZlecenia(ostatnieIDZleceniaUS));
		System.out.println("Dane zlecenie o ID " + ostatnieIDZleceniaUS + " : "+ ostatnieZlecenieUS.getText());
		Map<String,String> mapaMiesiecy = new TreeMap<String,String>();
		mapaMiesiecy.put("01", "styczeń");
		mapaMiesiecy.put("02", "luty");
		mapaMiesiecy.put("03", "marzec");
		mapaMiesiecy.put("04", "kwiecień");
		mapaMiesiecy.put("05", "maj");
		mapaMiesiecy.put("06", "czerwiec");
		mapaMiesiecy.put("07", "lipiec");
		mapaMiesiecy.put("08", "sierpień");
		mapaMiesiecy.put("09", "wrzesień");
		mapaMiesiecy.put("10", "październik");
		mapaMiesiecy.put("11", "listopad");
		mapaMiesiecy.put("12", "grudzień");
		 
		
//		Locale plLocale = new Locale("pl","PL");
//		DateFormat df = DateFormat.getDateInstance(DateFormat.DEFAULT,plLocale);
//		
//		DateFormat df1 = DateFormat.getDateInstance(DateFormat.LONG, plLocale);
//		String data1 = df1.format(df.parse("2000-" + daneZlecenieUS.get("Miesiąc") + "-01"));
//		System.out.println("Data: " + data1);
		
		if(daneZlecenieUS.get("Typ").equals("miesieczny")){
			daneWpisane = ostatnieIDZleceniaUS.toString().concat(" » \"" + daneZlecenieUS.get("Klient") + "\" » " + daneZlecenieUS.get("Rodzaj podatku") + " " + daneZlecenieUS.get("Rok") + " " + mapaMiesiecy.get(daneZlecenieUS.get("Miesiąc")) + " " + daneZlecenieUS.get("Kwota") + " " + daneZlecenieUS.get("Płatnik"));
		}
		else{
			daneWpisane = ostatnieIDZleceniaUS.toString().concat(" » \"" + daneZlecenieUS.get("Klient") + "\" » " + daneZlecenieUS.get("Rodzaj podatku") + " " + daneZlecenieUS.get("Rok") + " " + daneZlecenieUS.get("Kwartał") + " " + daneZlecenieUS.get("Kwota") + " " + daneZlecenieUS.get("Płatnik"));
		}
		
		System.out.println("Dane zlecenie o ID " + ostatnieIDZleceniaUS + " : " + daneWpisane);
		System.out.println("True: " + ostatnieZlecenieUS.getText().equals(daneWpisane));
		
		return ostatnieZlecenieUS.getText().equals(daneWpisane);
	}	
	
	public void usunZlecenieUSSkladkeZus(Integer idZleceniaUSSkladkiZus) {
		wait.until(ExpectedConditions.elementToBeClickable(UiMapping.passIDUsunZLecenieUS(idZleceniaUSSkladkiZus)));
		driver.findElement(UiMapping.passIDUsunZLecenieUS(idZleceniaUSSkladkiZus)).click();
		driver.switchTo().alert().accept();
	}
	
	public String pobierzIloscZlecenUsSkladekZus(){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.iloscZlecenUS));
		WebElement iloscZlecenUS = driver.findElement(UiMapping.iloscZlecenUS);
		return iloscZlecenUS.getText();
	}
	
	public void wypelnijSkladkeZus(Map<String,String> daneSkladkaZus, Boolean modyfikacja){	
		//Clear all fields
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.wyczyscPolaSklaskiZusBtn));
		WebElement wyczyscPolaBtn = driver.findElement(UiMapping.wyczyscPolaSklaskiZusBtn);
		wyczyscPolaBtn.click();
		
		//Fill in customer
		Select select;
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.skladkaZusWybierzKlientaBtn));
		WebElement commissionCustomerBtn = driver.findElement(UiMapping.skladkaZusWybierzKlientaBtn);
		commissionCustomerBtn.click();
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passSkladkaZusNazwaKlienta(daneSkladkaZus.get("Firma"))));
		WebElement commissionSelectCustomer = driver.findElement(UiMapping.passSkladkaZusNazwaKlienta(daneSkladkaZus.get("Firma")));
		commissionSelectCustomer.click();

		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.skladkaZusWybierzKlienta));
		WebElement comissionSetCustomer = driver.findElement(UiMapping.skladkaZusWybierzKlienta);
		comissionSetCustomer.click();
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.skladkaZusPolaInput));
		List<WebElement> polaTypuInput = driver.findElements(UiMapping.skladkaZusPolaInput);
		List<String> listaPol = new ArrayList<String>(daneSkladkaZus.keySet());

		System.out.println("Ilosc pol input: " + polaTypuInput.size());
		//Collections.sort(polaTypuInput);
		//polaTypuInput.
		for (int i = 0; i<(polaTypuInput.size()-1); i++){
			System.out.println("Pole: " + listaPol.get(i));
			polaTypuInput.get(i).clear();
			polaTypuInput.get(i).sendKeys(daneSkladkaZus.get(listaPol.get(i)));
		}
		
		//fill typ zlecenia
		if(!modyfikacja){
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passSkladkaZusTypZlecenia(daneSkladkaZus.get("Zlecenia"))));
			WebElement powiazanieZlecenia = driver.findElement(UiMapping.passSkladkaZusTypZlecenia(daneSkladkaZus.get("Zlecenia")));
			powiazanieZlecenia.click();
		}
		
		//fill in miesiąc
		select = new Select(driver.findElement(By.id("misiac")));
		select.selectByVisibleText(daneSkladkaZus.get("Miesiąc"));
		
		//fill in typ wplaty
		select = new Select(driver.findElement(By.id("typ")));
		select.selectByVisibleText(daneSkladkaZus.get("Typ wpłaty"));
		
		//fill in uwagi
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.zleceniaUSUwagi));
		WebElement uwagiZlecenia = driver.findElement(UiMapping.zleceniaUSUwagi);
		uwagiZlecenia.clear();
		uwagiZlecenia.sendKeys(daneSkladkaZus.get("Uwagi"));
		
	}
	
	public boolean zatwierdzSkladkeZus(Map<String,String> daneSkladkaZus){	
			//Dalej
			Boolean zgodnoscKomunikatow = false;
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.skladkaZusDodaj));
			WebElement skladkaZusDodaj = driver.findElement(UiMapping.skladkaZusDodaj);
			skladkaZusDodaj.click();
			
			//Sprawdz komunikat
			//Float sumaSkladek = Float.parseFloat(daneZlecenieUS.get("Kwota do zapłaty składka 51")) + Float.parseFloat(daneZlecenieUS.get("Kwota do zapłaty składka 52"))+ Float.parseFloat(daneZlecenieUS.get("Kwota do zapłaty składka 53"));
			
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.skladkaZusKomunikat));
			WebElement komunikat = driver.findElement(UiMapping.skladkaZusKomunikat);
			
			String komunikatWzor = "DEFINIUJESZ SKŁADKI UŻYTKOWNIKA \"" + daneSkladkaZus.get("Firma").toUpperCase() + "\" NA ROK \"" + daneSkladkaZus.get("Rok") + "\" MIESIAC \"" + daneSkladkaZus.get("Miesiąc") +
					"\" W NASTĘPUJACYCH KWOTACH: 51 - " + daneSkladkaZus.get("Kwota do zapłaty składka 51") + ", 52 - " + daneSkladkaZus.get("Kwota do zapłaty składka 52") + ", 53 - "+ daneSkladkaZus.get("Kwota do zapłaty składka 53") + "; SUMA SKŁADEK WYNOSI: " + daneSkladkaZus.get("Suma składek");
			
			zgodnoscKomunikatow = komunikat.getText().equals(komunikatWzor);
			
			System.out.println(komunikatWzor);
			System.out.println(komunikat.getText());
			
			//Dalej
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.skladkaZusDodaj2));
			skladkaZusDodaj = driver.findElement(UiMapping.skladkaZusDodaj2);
			skladkaZusDodaj.click();
			
			return zgodnoscKomunikatow;
	}
	
	public boolean zatwierdzZmodyfikowanaSkladkeZus(Map<String,String> daneSkladkaZus){	
		//Dalej
		Boolean zgodnoscKomunikatow = false;
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.zmodyfikowanaSkladkaZusDodaj));
		WebElement skladkaZusDodaj = driver.findElement(UiMapping.zmodyfikowanaSkladkaZusDodaj);
		skladkaZusDodaj.click();
		
		//Sprawdz komunikat
		//Float sumaSkladek = Float.parseFloat(daneZlecenieUS.get("Kwota do zapłaty składka 51")) + Float.parseFloat(daneZlecenieUS.get("Kwota do zapłaty składka 52"))+ Float.parseFloat(daneZlecenieUS.get("Kwota do zapłaty składka 53"));
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.skladkaZusKomunikat));
		WebElement komunikat = driver.findElement(UiMapping.skladkaZusKomunikat);
		
		String komunikatWzor = "MODYFIKUJESZ SKŁADKI UŻYTKOWNIKA \"" + daneSkladkaZus.get("Firma").toUpperCase() + "\" NA ROK \"" + daneSkladkaZus.get("Rok") + "\" MIESIAC \"" + daneSkladkaZus.get("Miesiąc") +
				"\" W NASTĘPUJACYCH KWOTACH: 51 - " + daneSkladkaZus.get("Kwota do zapłaty składka 51") + ", 52 - " + daneSkladkaZus.get("Kwota do zapłaty składka 52") + ", 53 - "+ daneSkladkaZus.get("Kwota do zapłaty składka 53") + "; SUMA SKŁADEK WYNOSI:" + daneSkladkaZus.get("Suma składek");
		
		zgodnoscKomunikatow = komunikat.getText().equals(komunikatWzor);
		
		System.out.println(komunikatWzor);
		System.out.println(komunikat.getText());
		
		//Dalej
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.zmodyfikowanaSladkaZusDodaj2));
		skladkaZusDodaj = driver.findElement(UiMapping.zmodyfikowanaSladkaZusDodaj2);
		skladkaZusDodaj.click();
		
		return zgodnoscKomunikatow;
}
	
	public Boolean sprawdzPolaSkladkaZus(Map<String,String> daneSkladkaZus){
		Boolean flagaZgodnosciZleceniaUS = true;
		String opisPola = "";
		String wartoscPola = "";
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.skladkaZusPolaDoSprawdzenia));
		List<WebElement> polaZleceniaUS = driver.findElements(UiMapping.skladkaZusPolaDoSprawdzenia);
		 
		for(int i=0; i<(polaZleceniaUS.size() - 3); i++){
			opisPola = polaZleceniaUS.get(i).getText().substring(0,polaZleceniaUS.get(i).getText().indexOf(":")).trim();			
			wartoscPola = polaZleceniaUS.get(i).getText().substring(polaZleceniaUS.get(i).getText().indexOf(":") + 1, polaZleceniaUS.get(i).getText().length()).trim();	

			System.out.println("Wartosc Pola: " + daneSkladkaZus.get(opisPola));
			System.out.println("Wartosc Pola: " + polaZleceniaUS.get(i).getText());
			
			if(i==6 || i==7 || i==8 || i == 9){
				System.out.println("True: " + wartoscPola.contains(daneSkladkaZus.get(opisPola)));
				flagaZgodnosciZleceniaUS &= wartoscPola.contains(daneSkladkaZus.get(opisPola));
			}
			else{
				System.out.println("True:" + daneSkladkaZus.get(opisPola).equals(wartoscPola));
				flagaZgodnosciZleceniaUS &= daneSkladkaZus.get(opisPola).equals(wartoscPola);
			}
		}
		return flagaZgodnosciZleceniaUS;
	}		
	
	public boolean sprawdzOstatniaSkladkeZus(Map<String,String> daneSkladkaZus){
		String daneWpisane = "";
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passIdUSZlecenia(ostatnieIDZleceniaUS)));
		WebElement ostatniaSkladkaZus = driver.findElement(UiMapping.passIdUSZlecenia(ostatnieIDZleceniaUS));
		System.out.println(ostatniaSkladkaZus.getText());
		Map<String,String> mapaTypowWplat = new TreeMap<String,String>();
		mapaTypowWplat.put("Opłata dodatkowa za błędy płatnika ", "A");
		mapaTypowWplat.put("Opłata dodatkowa", "D");
		mapaTypowWplat.put("Egzekucja", "E");
		mapaTypowWplat.put("Składka dłuższa niż 1 miesiąc", "M");
		mapaTypowWplat.put("Składka za jeden miesiąc", "S");
		mapaTypowWplat.put("Odroczenie terminu", "T");
		mapaTypowWplat.put("Układ ratalny", "U");

		daneWpisane = "»".concat(ostatnieIDZleceniaUS.toString() + " »" + daneSkladkaZus.get("Firma") + " " + daneSkladkaZus.get("Suma składek") + " " + daneSkladkaZus.get("Do kiedy zapłacić") + " " + daneSkladkaZus.get("Numer") + " " + daneSkladkaZus.get("Płatnik")+ "\n" 
				+ mapaTypowWplat.get(daneSkladkaZus.get("Typ wpłaty")));
		System.out.println(daneWpisane);

		System.out.println("True: " + ostatniaSkladkaZus.getText().contains((daneWpisane)));
		
		return ostatniaSkladkaZus.getText().trim().equals(daneWpisane);
	}
	
	public List<WebElement> pobierzListeFirm(){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.skladkaZusWybierzKlientaBtn));
		WebElement commissionCustomerBtn = driver.findElement(UiMapping.skladkaZusWybierzKlientaBtn);
		commissionCustomerBtn.click();
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.wszystkieFirmy));
		
		return driver.findElements(UiMapping.wszystkieFirmy);
		
	}
	
	public String[] wybierzFirme(WebElement f){
		String[] polaFirmy = {"",""}; 

		WebElement firma;
		System.out.println("Nazwa i nip firmy: " + f.getText());
		polaFirmy = f.getText().split("NIP: ");
	//	System.out.println("Span attr: " + By.xpath("//*[@id='pudelkoKlientAjax']/div/div/label/span[text()='Ekspert  2 -Bermar JST Test']/text()"));
		
		//System.out.println("Nazwa i nip firmy: " + f.getText());
		
		if(polaFirmy.length == 2){
			firma = driver.findElement(UiMapping.passFirmaNipSkladkaZus("NIP: " + polaFirmy[1].trim(), polaFirmy[0].trim()));
		}
		else {
			System.out.println("Nazwa firmy: " +  polaFirmy[0]);
			firma = driver.findElement(UiMapping.passFirmaNipSkladkaZus(polaFirmy[0]));
		}
	
		firma.click();
		
		return polaFirmy;
	}
	
	public void zatwierdzWybor(){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.skladkaZusWybierzKlienta));
		driver.findElement(UiMapping.skladkaZusWybierzKlienta).click();	
	}
	
	public boolean sprawdzWybor(String [] pF){
//		System.out.println("A: " + driver.findElement(UiMapping.nazwaNipFirmy).getText());
//		System.out.println("B: " + pF[0] + "NIP: " + pF[1]);
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.nazwaNipFirmy));
		System.out.println(driver.findElement(UiMapping.nazwaNipFirmy).getText().equals(pF[0] + "NIP: " + pF[1]));
		
		return driver.findElement(UiMapping.nazwaNipFirmy).getText().equals(pF[0] + "NIP: " + pF[1]);
	}
	
	public Boolean znajdzSkladkeZusPoId(int ID){	
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.listaSkladekZusBtn));
		driver.findElement(UiMapping.listaSkladekZusBtn).click();
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.stronyZlecenieUS));
		List<WebElement> stronySkladkiZus = driver.findElements(UiMapping.stronyZlecenieUS);
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.zleceniaUSNaStronie));
		List<WebElement> skladkiZus = driver.findElements(UiMapping.zleceniaUSNaStronie);
		System.out.println("ilosc stron" + stronySkladkiZus.size());

		for (int i = 0; i< stronySkladkiZus.size(); i++) {
			stronySkladkiZus.get(i).click();
			
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.stronyZlecenieUS));
			stronySkladkiZus = driver.findElements(UiMapping.stronyZlecenieUS);
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.zleceniaUSNaStronie));
			skladkiZus = driver.findElements(UiMapping.zleceniaUSNaStronie);
			
			for (WebElement skladkaZus : skladkiZus) {
				System.out.println("ID:" + skladkaZus.getText().trim());
				if(skladkaZus.getText().trim().equals(Integer.toString(ID))){
					System.out.println("Znaleziono skladke zus p ID" + ID + ":" + skladkaZus.getText().trim());
					skladkaZus.click();
					return true;
				}
			} 
		}	
		return false;
	}
	
	public void modyfikujSkladkeZus(){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.modyfikujSkladkeZusBtn));
		driver.findElement(UiMapping.modyfikujSkladkeZusBtn).click();	
	}
	
	public void filtrujZlecenieUSFirma(String nazwaFirmy){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.listaZlecenUS));
		driver.findElement(UiMapping.listaZlecenUS).click();	
		
		wait.until(ExpectedConditions.presenceOfElementLocated(By.id("firmy")));
		Select select = new Select(driver.findElement(By.id("firmy")));
		select.selectByVisibleText(nazwaFirmy);
	}
	
	public boolean sprawdzZleceniaUSPoFiltracjiFirma(String nazwaFirmy){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.stronyZlecenieUS));
		List<WebElement> stronySkladkiZus = driver.findElements(UiMapping.stronyZlecenieUS);
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.zleceniaUSNaStronie));
		List<WebElement> skladkiZus = driver.findElements(UiMapping.zleceniaUSNaStronie);
		System.out.println("ilosc stron " + stronySkladkiZus.size());

		for (int i = 0; i< stronySkladkiZus.size(); i++) {
			stronySkladkiZus.get(i).click();
			
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.stronyZlecenieUS));
			stronySkladkiZus = driver.findElements(UiMapping.stronyZlecenieUS);
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.wszystkieZleceniaUSNaStronie));
			skladkiZus = driver.findElements(UiMapping.wszystkieZleceniaUSNaStronie);
			
			for (WebElement skladkaZus : skladkiZus) {
				System.out.println("Nazwa firmy:" + skladkaZus.getText().trim());
				if(!skladkaZus.getText().trim().equals("\"" + nazwaFirmy + "\"")){
					System.out.println("Znaleziono skladke zus o nazwie firmy: " + skladkaZus.getText().trim());
					skladkaZus.click();
					return false;
				}
			} 
		}
		return true;
	}
	
	public Integer pobierzIloscZlecenFirmy(String nazwaFirmy){
		Integer iloscZlecenFirmy = 0;
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.listaZlecenUS));
		driver.findElement(UiMapping.listaZlecenUS).click();
		
		Select select = new Select(driver.findElement(By.id("firmy")));
		select.selectByVisibleText("Wszystkie");
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.stronyZlecenieUS));
		List<WebElement> stronySkladkiZus = driver.findElements(UiMapping.stronyZlecenieUS);
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.zleceniaUSNaStronie));
		List<WebElement> skladkiZus = driver.findElements(UiMapping.zleceniaUSNaStronie);
		System.out.println("ilosc stron " + stronySkladkiZus.size());

		for (int i = 0; i< stronySkladkiZus.size(); i++) {
			stronySkladkiZus.get(i).click();
			
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.stronyZlecenieUS));
			stronySkladkiZus = driver.findElements(UiMapping.stronyZlecenieUS);
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.wszystkieZleceniaUSNaStronie));
			skladkiZus = driver.findElements(UiMapping.wszystkieZleceniaUSNaStronie);
			
			for (WebElement skladkaZus : skladkiZus) {
				System.out.println("Nazwa firmy:" + skladkaZus.getText().trim());
				if(skladkaZus.getText().trim().equals("\"" + nazwaFirmy + "\"")){
					iloscZlecenFirmy++;
				}
			} 
		}
		
		return iloscZlecenFirmy;
	}
	
	public void wybierzOpcje(String opcja){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.opcje));
		WebElement ikonaPlus = driver.findElement(UiMapping.opcje);
		ikonaPlus.click();
		
		WebElement op = driver.findElement(UiMapping.przekazOpcje(opcja));
		wait.until(ExpectedConditions.elementToBeClickable(op));
		op.click();
	}
	
	public void wypelnijDaneUrzad(Map<String,String> daneUrzedu){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.polaInputUrzedu));
		List <WebElement> polaUrzad = driver.findElements(UiMapping.polaInputUrzedu);
		List<String> listaPol = new ArrayList<String>(daneUrzedu.keySet());

		for (int i = 0; i<(polaUrzad.size()); i++){
			polaUrzad.get(i).clear();
			polaUrzad.get(i).sendKeys(daneUrzedu.get(listaPol.get(i)));
		}
	}
	
	public void dodajUrzad(){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.dodajUrzadBtn));
		driver.findElement(UiMapping.dodajUrzadBtn).click();
	}
	
	public List<WebElement> znajdzUrzadPoNazwie(Map<String,String> daneUrzedu){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.przekazNazweUrzedu(daneUrzedu)));
		List <WebElement> listaUrzedow = driver.findElements(UiMapping.przekazNazweUrzedu(daneUrzedu));
		
		if(listaUrzedow.size() > 1){
			System.out.println("Znaleziono " + listaUrzedow.size() + " urzedy o nazwie " + daneUrzedu.get("Nazwa"));
		}	
		return listaUrzedow;
	}
	
	public boolean sprawdzPolaUrzedow(int iloscZnalezionychUrzedow, Map<String,String> daneUrzedu){
		String[] polaDoSprawdzeniaTab;
		Boolean flagaZgodnosciPolUrzedu = true;

			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.polaUrzeduDoSprawdzenia));
			List <WebElement> polaUrzedu = driver.findElements(UiMapping.polaUrzeduDoSprawdzenia);
			
			for(WebElement pole : polaUrzedu){
				polaDoSprawdzeniaTab = pole.getText().split(":");
				System.out.println("A" + polaDoSprawdzeniaTab[0]);
				System.out.println("B" + polaDoSprawdzeniaTab[1]);
				System.out.println(polaDoSprawdzeniaTab[1].trim().equals(daneUrzedu.get(polaDoSprawdzeniaTab[0].trim())));
				flagaZgodnosciPolUrzedu &= polaDoSprawdzeniaTab[1].trim().equals(daneUrzedu.get(polaDoSprawdzeniaTab[0].trim()));
			}
		return flagaZgodnosciPolUrzedu;
	}
	
	public void usunUrzad(Map<String,String> daneUrzedu){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.przekazNazweUrzedu(daneUrzedu)));
		List <WebElement> listaUrzedow = driver.findElements(UiMapping.przekazNazweUrzedu(daneUrzedu));
		Iterator<WebElement> iterator = listaUrzedow.iterator();
				
		while(iterator.hasNext()){
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.przekazNazweUrzedu(daneUrzedu)));
			listaUrzedow = driver.findElements(UiMapping.przekazNazweUrzedu(daneUrzedu));
			iterator = listaUrzedow.iterator();
			
			iterator.next().click();
			
			wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.usunBtn));
			driver.findElement(UiMapping.usunBtn).click();
			driver.switchTo().alert().accept();
		}
	}
	
	public void wypelnijDanePodatku(Map<String,String> daneNowegoPodatku) {
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.polaNowegoPodatku));
		List <WebElement> polaNowegoPodatku = driver.findElements(UiMapping.polaNowegoPodatku);
		//List<String> listaPol = new ArrayList<String>(daneNowegoPodatku.keySet());
		
		Iterator<WebElement> iteratorPola = polaNowegoPodatku.iterator();
		Iterator<String> iteratorDane = daneNowegoPodatku.keySet().iterator();
		
		while(iteratorPola.hasNext() && iteratorDane.hasNext()) {
			iteratorPola.next().sendKeys(daneNowegoPodatku.get(iteratorDane.next()));
		}
	}
	
	public void dodajNowyPodatek(){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.dodajNowyPodatekBtn));
		driver.findElement(UiMapping.dodajNowyPodatekBtn).click();
	}
	
	public Integer znajdzOstatniNowyPodatek(){
		Integer ostatnieIDNowegoPodatku = 0;
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.podatkiNaStronie));
		List<WebElement> podatki = driver.findElements(UiMapping.podatkiNaStronie);
			
		for (WebElement podatek : podatki) {
			ostatnieIDNowegoPodatku = Math.max(Integer.parseInt(podatek.getText().trim()),ostatnieIDNowegoPodatku);
		} 
		System.out.println("max:" + ostatnieIDNowegoPodatku);
		
		return ostatnieIDNowegoPodatku;
	}
	
	public boolean sprawdzOstatniPodatek(Integer idPodatku, Map<String,String> danePodatek){
		String daneWpisane = "";
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passIdPodatku(idPodatku)));
		WebElement ostatniPodatek = driver.findElement(UiMapping.passIdPodatku(idPodatku));
		System.out.println("Dane zlecenie o ID (odczytane) " + ostatnieIDZleceniaUS + " : "+ ostatniPodatek.getText());

		daneWpisane = idPodatku.toString().concat(" » " + danePodatek.get("Nazwa"));
		
		System.out.println("Dane zlecenie o ID (wpisane)   " + ostatnieIDZleceniaUS + " : " + daneWpisane);
		System.out.println("True: " + ostatniPodatek.getText().equals(daneWpisane));
		
		return ostatniPodatek.getText().equals(daneWpisane);
	}
	
	public boolean sprawdzOstatniPodatekWszystkiePola(Integer idPodatku, Map<String,String> danePodatek){
		boolean flagaZgodnosciPolPodatku = true;
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passIdNowegoPodatku(idPodatku)));
		driver.findElement(UiMapping.passIdNowegoPodatku(idPodatku)).click();
		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.polaNowegoPodatkuSprawdz));
		List <WebElement> polaNowegoPodatku = driver.findElements(UiMapping.polaNowegoPodatkuSprawdz);
		
		Iterator<WebElement> iteratorPola = polaNowegoPodatku.iterator();
		Iterator<String> iteratorDane = danePodatek.keySet().iterator();
		
		while(iteratorPola.hasNext() && iteratorDane.hasNext()) {
			flagaZgodnosciPolPodatku &= iteratorPola.next().getText().trim().equals(danePodatek.get(iteratorDane.next()));
			System.out.println("True: " + flagaZgodnosciPolPodatku);
		}
		
		return flagaZgodnosciPolPodatku;
	}
	
	public void usunPodatek(){	
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.usunBtn));
		driver.findElement(UiMapping.usunBtn).click();
		driver.switchTo().alert().accept();
	}
}
